/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.fspike.contentprovider.webprovider;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

/**
 *
 * @author Kenneth
 */
public class WikipediaContentProvider {

    private static final String wikiMediaURL = "http://species.wikimedia.org/wiki/--COMPOMICS--";
    private static final String wikiPediaURL = "http://en.wikipedia.org/wiki/--COMPOMICS--";

    /**
     *
     * @param term this term will be used to attempt to find a relevant
     * wikipedia page
     * @return an URL to the relevant page or throws a FileNotFound exception
     * if none exist
     * @throws MalformedURLException
     * @throws IOException
     * @throws ParserConfigurationException
     */
    public static URL getRelevantURL(String term) throws MalformedURLException, IOException, ParserConfigurationException {
        URL url = new URL(wikiMediaURL.replace("--COMPOMICS--", term.replace(" ", "%20")));
        return url;
    }

    /**
     *
     * @param term this term will be used to attempt to find a relevant
     * wikipedia page
     * @return a list of String (url) to the relevant page or throws a
     * FileNotFound exception if none exist
     * @throws MalformedURLException
     * @throws IOException
     * @throws ParserConfigurationException
     */
    public static List<String> getRelevantPicturesOnWikiMediaURL(String term) throws MalformedURLException, IOException, ParserConfigurationException {
        List<String> relevantImagesList = new LinkedList<>();
        String temp = (wikiMediaURL.replace("--COMPOMICS--", term.replace(" ", "%20")));
        Document doc = Jsoup.connect(temp).get();
        // get page title
        Elements images = doc.select("img");
        for (org.jsoup.nodes.Element aTopList : images) {
            relevantImagesList.add("http:" + aTopList.attr("src"));
        }
        return relevantImagesList;
    }

    public static List<String> getRelevantPicturesOnWikiPediaURL(String term) throws MalformedURLException, IOException, ParserConfigurationException {
        List<String> relevantImagesList = new LinkedList<>();
        String temp = (wikiPediaURL.replace("--COMPOMICS--", term.replace(" ", "%20")));
        Document doc = Jsoup.connect(temp).get();
        // get page title
        Elements images = doc.select("img");
        for (org.jsoup.nodes.Element aTopList : images) {
            relevantImagesList.add("http:" + aTopList.attr("src"));
        }
        return relevantImagesList;
    }

    public static void main(String args[]) throws Exception {
        System.out.println(getRelevantPicturesOnWikiPediaURL("Mus"));
        System.out.println(getRelevantPicturesOnWikiMediaURL("----------------"));
        System.out.println(getRelevantPicturesOnWikiMediaURL("Pan"));
    }

}
